package codebot.elector;
import java.util.ArrayList;

/**
 * This is the entity that will chose from the Candidates, based on the support
 * they have.
 * 
 * @author Teo
 * 
 */
public class Elector {

  /**
   * Candidates competing to be elected
   */
  private ArrayList<Candidate> mCandidates;

  /**
   * The appearance of an Elector
   */
  public Elector() {
    mCandidates = new ArrayList<Candidate>();
  }

  /**
   * Computes and elects the winner.
   * 
   * @return The winning Candidate.
   */
  public Candidate getWinner() {
    return (Candidate) Utils.getBestSupporter(mCandidates);
  }

  /**
   * Registers a Candidate among the ones the Elector will chose from.
   * 
   * @param candidate
   */
  public void registerCandidate(Candidate candidate) {
    if (!mCandidates.contains(candidate)) {
      mCandidates.add(candidate);
    }
  }

  /**
   * Tells this Elector that a Candidate doesn't compete anymore (there can be
   * any number of reasons - e.g. it now supports another candidate, it can't
   * handle what will be required of him, etc.)
   * 
   * @param candidate
   *          to retreat
   * @return false if the Candidate wasn't even registered in the first place
   */
  public boolean removeCandidate(Candidate candidate) {
    return mCandidates.remove(candidate);
  }
}
